package cn.rui.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.rui.pojo.vo.ChatBean;
import cn.rui.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 徽州大都督
 * @date 2021/6/15
 */
@RestController
@RequestMapping("chat")
@Api(tags = "websocket控制器")
public class WebSocketController {

    @Autowired
    private WebSocketServer webSocket;

    @PostMapping("/sendAllWebSocket")
    @ApiOperation(value = "发送消息给全部")
    public void sendAllWebSocket(@RequestBody ChatBean chatBean) {
        //返回消息
        chatBean.setStatus (1);
        //返回json字符串
        JSONObject json = JSONUtil.parseObj (chatBean, false, true);
        //System.out.println (json);
        webSocket.sendAllMessage (json.toStringPretty ());
    }

    @GetMapping("/sendOneWebSocket/{userName}")
    @ApiOperation(value = "发送消息给指定用户")
    public String sendOneWebSocket(@PathVariable("userName") String userName) {
        String text = userName + " 你好！ 这是websocket单人发送！";
        webSocket.sendOneMessage (userName, text);
        return text;
    }
}
